package com.proactive.webapp.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import com.proactive.webapp.dao.WorksheetContentDAO;
import com.proactive.webapp.entity.Member;
import com.proactive.webapp.entity.WorksheetContent;
import com.proactive.webapp.util.DateConversion;
import com.proactive.webapp.util.EncodeHtml;

public class WorksheetContentDAOImpl implements WorksheetContentDAO{
	DateConversion dateConversion = new DateConversion();
	EncodeHtml encodeHtml = new EncodeHtml();
	private DataSource dataSource;
	private Connection conn = null ;
	private ResultSet rs = null ;
	private PreparedStatement smt = null ;
	
	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}

	@Override
	public List<WorksheetContent> getList(int worksheetID) {
		// TODO Auto-generated method stub
		List<WorksheetContent> contentList = new ArrayList<WorksheetContent>();
		String sql = "SELECT * FROM worksheet_content WHERE worksheetID = ? "
				+ "ORDER BY createDate";
		try {
			conn = dataSource.getConnection();
			smt = conn.prepareStatement(sql);
			smt.setInt(1, worksheetID);
			rs = smt.executeQuery();
			while(rs.next()){
				WorksheetContent worksheetContent = new WorksheetContent();
				worksheetContent.setId(rs.getInt("worksheetContentID"));
				worksheetContent.setTitle(rs.getString("worksheetContentTitle"));
				worksheetContent.setUrl(rs.getString("worksheetContentUrl"));
				worksheetContent.setContent(rs.getString("worksheetContent"));
				worksheetContent.setContentShow(worksheetContent.getContent().replaceAll("(\r\n|\n)", "<br>"));
				contentList.add(worksheetContent);
			}
			rs.close();
			smt.close();
 
		} catch (SQLException e) {
			throw new RuntimeException(e);
 
		} finally {
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {}
			}
		}
		return contentList;
	}

	@Override
	public void insert(WorksheetContent worksheetContent) {
		// TODO Auto-generated method stub
		String sql = "INSERT INTO worksheet_content(worksheetID, worksheetContentTitle, "
				+ "worksheetContentUrl, worksheetContent, memberAccount, createDate) "
				+ "VALUES(?,?,?,?,?,NOW())";
		try {
			conn = dataSource.getConnection();
			smt = conn.prepareStatement(sql);
			smt.setInt(1, worksheetContent.getWid());
			smt.setString(2, worksheetContent.getTitle());
			smt.setString(3, worksheetContent.getUrl());
			smt.setString(4, worksheetContent.getContent());
			smt.setString(5, worksheetContent.getAccount());
			smt.executeUpdate();
			smt.close();
 
		} catch (SQLException e) {
			throw new RuntimeException(e);
 
		} finally {
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {}
			}
		}
	}

	@Override
	public void delete(int worksheetContentID) {
		// TODO Auto-generated method stub
		String sql = "DELETE FROM worksheet_content WHERE worksheetContentID = ?";
		try {
			conn = dataSource.getConnection();
			smt = conn.prepareStatement(sql);
			smt.setInt(1, worksheetContentID);
			smt.executeUpdate();
			smt.close();
 
		} catch (SQLException e) {
			throw new RuntimeException(e);
 
		} finally {
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {}
			}
		}
	}

	@Override
	public void update(WorksheetContent worksheetContent) {
		// TODO Auto-generated method stub
		String sql = "UPDATE worksheet_content SET worksheetContentTitle=?, "
				+ "worksheetContentUrl=?, worksheetContent=? WHERE worksheetContentID=?";
		try {
			conn = dataSource.getConnection();
			smt = conn.prepareStatement(sql);
			smt.setString(1, worksheetContent.getTitle());
			smt.setString(2, worksheetContent.getUrl());
			smt.setString(3, worksheetContent.getContent());
			smt.setInt(4, worksheetContent.getId());
			smt.executeUpdate();
			smt.close();
 
		} catch (SQLException e) {
			throw new RuntimeException(e);
 
		} finally {
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {}
			}
		}
	}

	@Override
	public WorksheetContent get(WorksheetContent worksheetContent) {
		// TODO Auto-generated method stub
		String sql = "SELECT * FROM worksheet_content WHERE worksheetContentID = ?";
		try {
			conn = dataSource.getConnection();
			smt = conn.prepareStatement(sql);
			smt.setInt(1, worksheetContent.getId());
			rs = smt.executeQuery();
			while(rs.next()){
				worksheetContent.setTitle(rs.getString("worksheetContentTitle"));
				worksheetContent.setUrl(rs.getString("worksheetContentUrl"));
				worksheetContent.setContent(rs.getString("worksheetContent"));
				worksheetContent.setAccount(rs.getString("memberAccount"));
			}
			rs.close();
			smt.close();
 
		} catch (SQLException e) {
			throw new RuntimeException(e);
 
		} finally {
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {}
			}
		}
		return worksheetContent;
	}

}
